<template>
  <CURDDialog
    :create-title="t('aiot.comment.visitInfo')"
    :show-options="showOptions"
    :model="model"
    :model-form-setting="modelFormSetting"
    :model-rules="modelRules"
    :curd="options ? (options.workOrderComplaintId ? complainCurd : orderCurd) : undefined"
    :hide-next-add-btn="true"
    @before-ok="beforeVisit"
    @ok="$emit('ok')"
  />
</template>
<script setup lang="ts">
import { ref, watch } from "vue";
import { Api } from "/@/api/aiot/serviceWorkOrder";
import {
  CURDAction,
  CURDModalEvent,
  FormItemComponentType,
  FormItemData,
  ShowCURDModalOptions,
} from "./CURDDialog/models";
import { VisitRecordItem } from "/@/api/aiot/commentManage/models";
import { useI18n } from "/@/locales/useI18n";
import { VisitModalOptions } from "/@/types/components";
import { useCURD } from "/@/hooks/useCURD";

const { t } = useI18n();
const props = defineProps<{
  options?: VisitModalOptions;
}>();
defineEmits(["ok"]);

watch(
  () => props.options,
  (v) => {
    if (!v) return;
    showOptions.value = { action: CURDAction.CREATE };
  }
);

const showOptions = ref<ShowCURDModalOptions>();

const { curd: orderCurd } = useCURD({
  createUrl: Api.addWorkOrderVisitRecords,
});
const { curd: complainCurd } = useCURD({
  createUrl: Api.addWorkOrderComplaintVisitRecords,
});

const model = ref<VisitRecordItem>({
  visitRecordTitle: "",
  visitRecordContent: "",
  visitRecordSuggestion: "",
});
const modelRules = ["visitRecordTitle", "visitRecordContent", "visitRecordSuggestion"];
const modelFormSetting: FormItemData<VisitRecordItem>[] = [
  {
    key: "visitRecordTitle",
    label: t("aiot.serviceOrder.visitRecordTitle"),
    max: 64,
    props: {
      placeholder: t("common.pleaseInput"),
    },
  },
  {
    key: "visitRecordContent",
    label: t("aiot.serviceOrder.visitRecordContent"),
    max: 256,
    type: FormItemComponentType.TEXTAREA,
    props: {
      placeholder: t("common.pleaseInput"),
    },
  },
  {
    key: "visitRecordSuggestion",
    label: t("aiot.serviceOrder.visitRecordSuggestion"),
    max: 256,
    type: FormItemComponentType.TEXTAREA,
    props: {
      placeholder: t("common.pleaseInput"),
    },
  },
];
function beforeVisit(e: CURDModalEvent) {
  if (e.model) {
    Object.assign(e.model, props.options);
  }
}
</script>
